package com.gst.handfood.controller.fragment;

import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.gst.handfood.R;
import com.gst.handfood.base.BaseFragment;
import com.gst.handfood.controller.activity.SearchDemo;
import com.gst.handfood.model.bean.SearchBeanByName;
import com.loopj.android.http.RequestParams;

import butterknife.Bind;
import butterknife.OnClick;

import static com.gst.handfood.R.id.et_search;
import static com.gst.handfood.utils.Constant.APPKEY;

/**
 * Created by YBH on 2017/10/31.
 */
public class HomeFragment extends BaseFragment {
    @Bind(R.id.iv_add)
    ImageView ivAdd;
    @Bind(et_search)
    EditText etSearch;
    @Bind(R.id.iv_speack)
    ImageView ivSpeack;
    @Bind(R.id.buy)
    ImageView buy;
    @Bind(R.id.rv_home_content)
    RecyclerView rvHomeContent;
    @Bind(R.id.ib_top)
    ImageButton ibTop;
    private String s;

    @Override
    protected void initData(String content) {
        Log.e("ff","initData");
        parseJson(content);
    }

    private void parseJson(String json) {
        if (!TextUtils.isEmpty(json)) {
            SearchBeanByName searchBeanByName = JSON.parseObject(json, SearchBeanByName.class);
            if(searchBeanByName.getReason().equals("Success")){
                SearchBeanByName.ResultBean result = searchBeanByName.getResult();
                String id = result.getData().get(0).getImtro();
                Log.e("parse","解析成功=="+ id);
            }
        }
    }

    @Override
    protected void initTiTle() {
        Log.e("ff","initTiTle");
    }

    @Override
    protected RequestParams getParams() {
        Log.e("ff","getParams");
        //西红柿
        RequestParams params = new RequestParams();
        params.put("menu",s);//需要查询的菜谱名
        params.put("key",APPKEY);//应用APPKEY(应用详细页查询)
        params.put("dtype","");//返回数据的格式,xml或json，默认json
        params.put("pn","3");//数据返回起始下标
        params.put("rn","30");//数据返回条数，最大30
        params.put("albums","");//albums字段类型，1字符串，默认数组
        return params;
    }

    @Override
    public int getLayoutId() {
        Log.e("ff","getLayoutId");
        return R.layout.fragment_home;
    }

    @Override
    protected String getUrl() {
        Log.e("ff","getUrl");
//        return Constant.Base;
        return "";
    }


    @OnClick({R.id.iv_add, et_search, R.id.iv_speack, R.id.buy,R.id.ib_top})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_add:
                break;
            case et_search:
                startActivity(new Intent(getActivity(), SearchDemo.class));
                break;
            case R.id.iv_speack:
                break;
            case R.id.buy:
                break;
            case R.id.ib_top://置顶
                rvHomeContent.scrollToPosition(0);
                break;
        }
    }

}
